import { ref } from "vue";
import { defineStore } from "pinia";
import dayjs from "dayjs";

export const useChatStore = defineStore("chat", () => {
  const message = ref<ChatMessageType[]>([]);
  const addMessage = (msg: ChatMessageType) => {
    msg.id = message.value.length + 1;
    msg.date = dayjs(Date.now()).format("YYYY-MM-DD HH:mm:ss");
    message.value.push(msg);
  }
  const removeMessage = (ids: number[]) => {
    message.value = message.value.filter((item) => ids.findIndex((it) => it === item.id) != -1);
  }
  return {
    message,
    addMessage,
    removeMessage,
  };
}, {
  persist: {
    storage: localStorage,
  }
});